package com.funcity.stage.layers
{
	import com.funcity.stage.elements.GroundTile;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	
	public final class GroundLayer extends Sprite
	{
		private static const TILE_DIMENSION:uint = 3;
		private static const TILE_WIDTH:int = 500;
		private static const TILE_HEIGHT:int = 300;
		
		private var _dimension:uint = TILE_DIMENSION;
		private var _horizontalDimension:uint = 3;
		private var _verticalDimension:uint = 3;

		
		private var _tiles:Vector.<GroundTile> = new Vector.<GroundTile>();
		
		private var _mapURL:String;
		private var _xOriginG:uint = 0;
		private var _yOriginG:uint = 0;
		
		private var _curPix:Point = new Point;
		private var _curGrid:Point = new Point;
		
		public function GroundLayer()
		{
			super();
			mouseChildren = false;
			mouseEnabled = false;
			
			var initInd:uint = 0;
			addEventListener(Event.ADDED_TO_STAGE, 
			function init(e:Event):void
			{
				initInd = ++initInd % 2;
				if(initInd == 1) return;
				
				load(); // add ground tiles
				
				stage.addEventListener(Event.RESIZE,
				function resize(e:Event):void
				{
					load();
				});
			});
		}
		
		public function set scale(value:Number):void
		{
			if (value < 0.5 || value > 1)
			{
				return;
			}
			// set new dimension
			_dimension = Math.ceil(TILE_DIMENSION / value);
			
			// clear existed tiles and reset tiles
			load();
			
			// set ground scale
			scaleX = scaleY =  value;
		}
		
		public function changeMap(mapURL:String, xcP:uint, ycP:uint):void
		{
			_curPix.x = xcP;
			_curPix.y = ycP;
			_curGrid = pToG(_curPix);
			
			for each (var tile:GroundTile in _tiles)
			{
				tile.changeMap(mapURL, xcP, ycP);
			}
			
			_mapURL = mapURL;
		}
		
		public function move(xDirect:int, yDirect:int):void
		{
			for each (var tile:GroundTile in _tiles)
			{
				tile.move(xDirect, yDirect);
			}
			
			_xOriginG += xDirect;
			_yOriginG += yDirect;
		}
		
		private function calDimension():void
		{
			_horizontalDimension = Math.ceil(stage.stageWidth / TILE_WIDTH) + 2;
			_verticalDimension = Math.ceil(stage.stageHeight / TILE_HEIGHT) + 2;
		}
		
		private function needReload():Boolean
		{
			if( _horizontalDimension != Math.ceil(stage.stageWidth / TILE_WIDTH) + 2 ||
				_verticalDimension != Math.ceil(stage.stageHeight / TILE_HEIGHT) + 2)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
		
		//场景坐标为xcp和ycp的位置，作为视野中心
		public function centerTo(xcP:Number, ycP:Number):void
		{	
			_curPix.x = xcP;
			_curPix.y = ycP;
			
			_curGrid = pToG(_curPix);
			
			for each(var tile:GroundTile in _tiles)
			{
				tile.centerTo(xcP, ycP);
			}
		}
		
		//场景像素坐标转为格子坐标
		private function pToG(pix:Point):Point
		{
			var grid:Point = new Point;
			grid.x = uint(pix.x / TILE_WIDTH);
			grid.y = uint(pix.y / TILE_HEIGHT);
			return grid;
		}
		
		//获取中心格子在视野中的像素坐标
		public function centerGridPixInView():Point
		{
			var centerX:Number = stage.stageWidth / 2;
			var centerY:Number = stage.stageHeight / 2;
			
			var centerGridX:Number = centerX - (_curPix.x - _curGrid.x * TILE_WIDTH);
			var centerGridY:Number = centerY - (_curPix.y - _curGrid.y * TILE_HEIGHT);
			
			return new Point(centerGridX, centerGridY);
		}
		
		//获取中心格子的在视野中的格子坐标
		public function centerGridGridInView():Point
		{
			var x:Number = uint(_horizontalDimension / 2);
			var y:Number = uint(_verticalDimension / 2);
			return new Point(x,y);
		}
		
		//从某个格子在视野中的格子坐标获得像素坐标
		public function gridPixInView(gridInView:Point):Point
		{
			var cgg:Point = centerGridGridInView();
			var cgp:Point = centerGridPixInView();
			
			var x:Number = cgp.x + (gridInView.x - cgg.x) * TILE_WIDTH;
			var y:Number = cgp.y + (gridInView.y - cgg.y) * TILE_HEIGHT;
			return new Point(x, y);
		}
		

		private function load():void
		{
			while (_tiles.length != 0)
			{
				var tile:GroundTile = _tiles.pop();
				removeChild(tile);
				//TODO TILE.CLEAR()  info
			}
			
			calDimension();
			
			for (var xCoord:uint = 0; xCoord < _horizontalDimension; xCoord++)
			{
				for (var yCoord:uint = 0; yCoord < _horizontalDimension; yCoord++)
				{
					var tile:GroundTile = new GroundTile(_horizontalDimension, _verticalDimension, TILE_WIDTH, TILE_HEIGHT, xCoord, yCoord);
					addChild(tile);
					_tiles.push(tile);
					
					if(_mapURL)
					{
						tile.changeMap(_mapURL, _curPix.x, _curPix.y);
					}
				}
			}
		}
	}
}